//
//  BYShopCartCell.m
//  OCDemo
//
//  Created by zhumuzhi on 2022/1/5.
//

#import "BYShopCartCell.h"
#import "BYCountTextField.h"
#import "BYShopCartModel.h"

@interface BYShopCartCell ()

// 背景图
@property (nonatomic, strong) UIView            *backGroundView;
// 是否选中
@property (nonatomic, strong) UIButton          *selectedButton;
// 商品图片
@property (nonatomic, strong) UIImageView       *goodImage;
// 商品标题
@property (nonatomic, strong) UILabel           *goodTitle;
// 商品价格
@property (nonatomic, strong) UILabel           *goodPrice;
// 操作按钮
@property (nonatomic, strong) BYCountTextField  *countSelectView;

@end

@implementation BYShopCartCell


#pragma mark - SetData

- (void)setModel:(BYShopCartModel *)model {
    _model = model;
    
    self.goodImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", model.product_iconimage]];
    self.goodTitle.text = model.product_name;
    self.goodPrice.text = model.product_price;
    self.countSelectView.count = model.number.integerValue;
}

#pragma mark - Event
- (void)selectedEvent {
    self.selectedButton.selected = !self.selectedButton.selected;
}

#pragma mark - CellHeight
+ (CGFloat)cellHeight {
    return kAutoWithSize(100);
}

#pragma mark - Init

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = BACKGROUND_VIEW_COLOR;
        [self.contentView addSubview:self.backGroundView];
        [self.backGroundView addSubview:self.selectedButton];
        [self.backGroundView addSubview:self.goodImage];
        [self.backGroundView addSubview:self.goodTitle];
        [self.backGroundView addSubview:self.countSelectView];
        [self.backGroundView addSubview:self.goodPrice];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.backGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(kAutoWithSize(10));
        make.right.equalTo(self.contentView).offset(-kAutoWithSize(10));
        make.bottom.equalTo(self.contentView);
    }];
    
    [self.selectedButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backGroundView).offset(kAutoWithSize(10));
        make.top.equalTo(self.backGroundView);
        make.bottom.equalTo(self.backGroundView);
        make.width.mas_equalTo(kAutoWithSize(16));
    }];
    
    [self.goodImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backGroundView).offset(kAutoWithSize(10));
        make.left.equalTo(self.selectedButton.mas_right).offset(kAutoWithSize(10));
        make.width.height.mas_equalTo(kAutoWithSize(72));
    }];
    
    [self.goodTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backGroundView).offset(kAutoWithSize(10));
        make.left.equalTo(self.goodImage.mas_right).offset(kAutoWithSize(10));
        make.right.equalTo(self.backGroundView.mas_right).offset(-kAutoWithSize(10));
    }];
    
    [self.countSelectView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodTitle.mas_bottom).offset(kAutoWithSize(10));
        make.right.equalTo(self.backGroundView).offset(-kAutoWithSize(10));
        make.width.mas_equalTo(kAutoWithSize(100));
        make.height.mas_equalTo(kAutoWithSize(22));
    }];
    
    [self.goodPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.countSelectView);
        make.left.equalTo(self.goodTitle);
    }];
    
    [super layoutSubviews];
}

#pragma mark - LazyGet

- (UIView *)backGroundView {
    if (!_backGroundView) {
        _backGroundView = [[UIView alloc] init];
        _backGroundView.backgroundColor = [UIColor whiteColor];
        _backGroundView.layer.cornerRadius = 8.0f;
    }
    return _backGroundView;
}

- (UIButton *)selectedButton{
    if (!_selectedButton) {
        _selectedButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_selectedButton setImage:[UIImage imageNamed:@"check_selected"] forState:UIControlStateSelected];
        [_selectedButton setImage:[UIImage imageNamed:@"check_normal"] forState:UIControlStateNormal];
        [_selectedButton addTarget:self action:@selector(selectedEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectedButton;
}

- (UIImageView *)goodImage{
    if (!_goodImage) {
        _goodImage = [[UIImageView alloc]init];
    }
    return _goodImage;
}

- (UILabel *)goodTitle{
    if (!_goodTitle) {
        _goodTitle = [[UILabel alloc]init];
        _goodTitle.font = MZBoldFont(13);
        _goodTitle.textColor = [UIColor blackColor];
        _goodTitle.text = @"商品标题";
    }
    return _goodTitle;
}

- (UILabel *)goodPrice{
    if (!_goodPrice) {
        _goodPrice = [[UILabel alloc]init];
        _goodPrice.font = MZFont(13);
        _goodPrice.textColor = [UIColor redColor];
        _goodPrice.text = @"¥241";
    }
    return _goodPrice;
}

- (BYCountTextField *)countSelectView{
    if (!_countSelectView) {
        _countSelectView = [[BYCountTextField alloc] init];
        [_countSelectView.textField setViewCornerRadius:5.0];
        _countSelectView.textField.backgroundColor = UIColorFromRGB(0xF5F5F5);
        _countSelectView.leftButton.backgroundColor = [UIColor whiteColor];
        _countSelectView.rightButton.backgroundColor = [UIColor whiteColor];
        _countSelectView.maxCount = 999;
    }
    return _countSelectView;
}

@end
